-- CIS 552, Mini-project
-- by Mike Gottlieb and Reilly Grant

-- |Current state for a player.
module Player (T, -- type
               player, -- constructor
               name, hand, money, inPlay, -- accessors
               bets, dealTo, draws, discards, folds, reset -- functions
              ) where

import Data.List

import qualified Card
import qualified Deck
import qualified Hand

data T = Player {name   :: String,
                 hand   :: [Card.T],
                 money  :: Int,
                 inPlay :: Bool
                }

-- |Pretty printing of a Player
instance Show T where
    show p = let cardPrep = zip [1..] $
                              map (\s -> " " ++ (show s) ++ "\n") $ hand p
                 cardShow = unwords $ map pairShow cardPrep
                 pairShow (num, string) =
                     (Card.showCard $ (hand p) !! (num - 1)) ++ " %" ++ string in
             "Player " ++ (name p) ++ "\n" ++
             "Hand:\n " ++ cardShow ++
             "Your hand: " ++ show (Hand.rankHand $ hand p) ++

             "\nMoney: " ++ (show $ money p) ++ "\n" ++
             
             if inPlay p then "" else "Not in play.\n"


defaultMoney :: Int
defaultMoney = 1024

-- |Construct a new player.
player :: String -> T
player n = Player {name   = n,
                   hand   = [],
                   money  = defaultMoney,
                   inPlay = True
                  }

-- |Subtract the amount bet from the player's money.
bets :: T -> Int -> T
bets p a = p { money = money p - a }

-- |Replace the player's hand with the dealt one.
dealTo :: T -> [Card.T] -> T
dealTo p h = p { hand = h }

-- |Draw n cards from the deck into the player's hand.
draws :: T -> Deck.T -> Int -> (T, Deck.T)
draws p d n = (p', d')
    where (cs, d') = Deck.deal d n
          p' = dealTo p (hand p ++ cs)

-- |Discard cards from the player's hand.
discards :: T -> [Card.T] -> T
discards p cs = p { hand = hand p \\ cs }

-- |The player folds.
folds :: T -> T
folds p = p { inPlay = False }

-- |Reset the player to a pre-play state. (With money preserved.)
reset :: T -> T
reset p = p { hand = [], inPlay = True }
